package com.xc.hotelmgr.datahandlermt.service.mt.impl;

import com.xc.hotelmgr.datahandlermt.pojo.mq.*;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.DtStore;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.OpTaskStatus;
import com.xc.hotelmgr.datahandlermt.service.mt.HotelToMQService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.DtStoreService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.DtUpHtlService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.OpTaskStatusService;
import com.xc.hotelmgr.datahandlermt.utils.MqType;
import com.xc.hotelmgr.datahandlermt.utils.SendMessageMgr;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class HotelToMQServiceImpl implements HotelToMQService {

    private static final String STORE_HOTEL_TASK_CODE = "STOREHOTEL";

    private static final String STORE_HOTEL_TASK_NAME = "分销商店铺酒店推送";

    private static final String RP_TASK_NAME = "分销商价格库存推送";

    private static final String STORE_HOTEL_STATE_NAME = "分销商店铺已上架酒店状态变更";

    private final DtUpHtlService dtUpHtlService;

    private final SendMessageMgr sendMessageMgr;

    private final OpTaskStatusService opTaskStatusService;

    private final DtStoreService dtStoreService;


    /**
     * 店铺酒店全部上下架
     * @param storeId
     * @param status
     */
    @Async
    @Override
    public void upDown(int storeId, int status) {

        List<Long> list = dtUpHtlService.getByStoreId(storeId);
        if(!list.isEmpty()) {
            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            int num = list.size(); // 执行总数量

            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);
            Long taskId = opTaskStatus.getId();  // 任务id
            list.add(Long.parseLong("-1"));  // 结束判断标识

            for (Long hotelId : list) {
                UpDown upDown = new UpDown();
                upDown.setHotelId(hotelId);
                upDown.setStatus(status);
                upDown.setStoreId(storeId);
                upDown.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HTUPDOWN, upDown, appKey);
            }
        }
    }

    /**
     * 店铺选定酒店上下架
     * @param upDownList
     */
    @Async
    @Override
    public void upDownList(UpDownList upDownList) {
        List<Long> hotelList = upDownList.getHotelList();
        if(!hotelList.isEmpty()) {
            Integer storeId = upDownList.getStoreId();
            Integer status = upDownList.getStatus();
            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            int num = hotelList.size(); // 执行酒店数量

            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);

            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);
            Long taskId = opTaskStatus.getId();
            hotelList.add(Long.parseLong("-1")); // 结束判断标识

            for (Long hotelId : hotelList) {
                UpDown upDown = new UpDown();
                upDown.setHotelId(hotelId);
                upDown.setStatus(status);
                upDown.setStoreId(storeId);
                upDown.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HTUPDOWN, upDown, appKey);
            }
        }
    }

    /**
     * 店铺酒店全部推送
     * @param storeId
     */
    @Async
    @Override
    public void sendAll(int storeId) {
        List<Long> list = dtUpHtlService.getByStoreId(storeId);
        if(!list.isEmpty()) {
            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            int num = list.size();
            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);  // 生成新的任务
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }

    /**
     * 店铺酒店选定推送
     * @param sendAllList
     */
    @Async
    @Override
    public void sendAllList(SendAllList sendAllList) {
        List<Long> list = sendAllList.getHotelList();
        if(!list.isEmpty()) {
            int storeId = sendAllList.getStoreId();
            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            int num = list.size();

            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }

    @Override
    public void sendRs(SendRs sendRs) {
        Integer storeId = sendRs.getStoreId();
        Integer sendType = sendRs.getSendType();
        //当 sendType = 2 或者 3 时，sendId 为地区编码，当 sendId = 4 或 5时，sendId 为分组编码
        Long sendId = sendRs.getSendId();
        List<Long> list = null;
        switch (sendType){
            case 2:
            case 3:
                list = dtUpHtlService.getByAreaId(storeId,sendId);
                break;
            case 4:
            case 5:
                list = dtUpHtlService.getByGroupId(storeId,sendId);
                break;
            default:
                list = dtUpHtlService.getByStoreId(storeId);
        }

        if(!list.isEmpty()) {
            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            int num = list.size();
            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(RP_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);  // 生成新的任务
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendAll.setSendType(sendType);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }
}
